package com.tentact.web1216.dao;



import com.tentact.web1216.vo.Clazz;
import com.tentact.web1216.vo.Student;

import java.sql.SQLException;
import java.util.List;

public class StudentDao {


    //查询
    public List<Student> selectStudent(Student student) {
        String sql = "SELECT id,name,clazzid from  student  where name like ? and clazzid like ? ";

        Object[] params = {student.getName(), student.getClazzid()};
        RowMapper<Student> rm = (rs) -> {

            Student students = new Student();

            try {
                students.setId(rs.getInt("id"));
                students.setName(rs.getString("name"));
               students.setClazzid(rs.getInt("clazzid"));


            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return students;
        };
        return Dbutil.executeQuery(sql, params, rm);
    }
    //查询全部
    public List<Student> selectBynameStudent(String name) {
        String sql = "SELECT id,name,clazzid from  student  where name like ? ";

        Object[] params = {"%"+name+"%"};
        RowMapper<Student> rm = (rs) -> {

            Student students = new Student();


            try {
                students.setId(rs.getInt("id"));
                students.setName(rs.getString("name"));
                students.setClazzid(rs.getInt("clazzid"));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }


            return students;
        };
        return Dbutil.executeQuery(sql, params, rm);
    }

    //添加
    public int insertStudent(Student student) {
        String sql = "insert into student(name,clazzid,sex,birthday) VALUES(?,?,?,?)";
        Object[] params = {student.getName(), student.getClazzid(), student.getSex(), student.getBirthday()};
        return Dbutil.executUdate(sql, params);
    }

    //根据学号查询
    public Student selectPrimaryStudent(int id) {
        String sql = "SELECT s.id,s.name,c.id from clazz c JOIN student s on c.id=s.clazzid where s.id=? ";
        Object[] params = {id};
        RowMapper<Student> rm = (rs) -> {
            Clazz clazz = new Clazz();
            Student students = new Student();

            try {
                students.setId(rs.getInt("id"));
                students.setName(rs.getString("name"));
                clazz.setId(rs.getInt("Id"));
                students.setClazz(clazz);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return students;
        };
        return Dbutil.executeQuerySingle(sql, params, rm);
    }

    //修改
    public int updateStudent(Student student){
        String sql="UPDATE student set name=?,clazzid=?,sex=?,birthday=? where id=?";
        Object[] params={student.getName(),student.getClazzid(),student.getSex(),student.getBirthday(),student.getId()};
        return Dbutil.executUdate(sql,params);
    }
    //删除
    public int deleteById(int id){
        String sql="UPDATE student set name=?,clazzid=?,sex=?,birthday=? where id=?\n" +
                "delete from student where id=? ";
        Object[] params={id};
        return  Dbutil.executUdate(sql,params);
    }
}
